package com.ifuncat.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ifuncat.gulimall.common.utils.PageUtils;
import com.ifuncat.gulimall.common.utils.Query;
import com.ifuncat.gulimall.common.utils.R;
import com.ifuncat.gulimall.ware.dao.WareSkuDao;
import com.ifuncat.gulimall.ware.entity.WareSkuEntity;
import com.ifuncat.gulimall.ware.feign.ProductFeignService;
import com.ifuncat.gulimall.ware.service.WareSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
    @Autowired
    private WareSkuDao wareSkuDao;
    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();

        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId) && !"0".equals(skuId)) {
            queryWrapper.eq("sku_id", skuId);
        }

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId) && !"0".equals(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }

        IPage<WareSkuEntity> page = this.page(new Query<WareSkuEntity>().getPage(params), queryWrapper);

        return new PageUtils(page);
    }

    /**
     * 添加库存
     */
    @Override
    public void addStock(WareSkuEntity entity) {
        //查询是否存在,不存在则新增,否则修改原来记录
        List<WareSkuEntity> entityList = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", entity.getSkuId()).eq("ware_id", entity.getWareId()));
        if (!CollectionUtils.isEmpty(entityList)) {
            WareSkuEntity wareSkuEntity = entityList.get(0);
            wareSkuEntity.setStock(wareSkuEntity.getStock() + entity.getStock());
            wareSkuDao.update(wareSkuEntity, new UpdateWrapper<WareSkuEntity>().eq("sku_id", entity.getSkuId()).eq("ware_id", entity.getWareId()));
        } else {
            entity.setStockLocked(0);
            entity.setSkuName(getSkuName(entity.getSkuId()));
            wareSkuDao.insert(entity);
        }
    }

    /**
     * 远程查询sku的名字，如果失败整个事务无需回滚
     */
    private String getSkuName(Long skuId) {
        try {
            R info = productFeignService.info(skuId);
            Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
            if (info.getCode() == 0) {
                return (String) data.get("skuName");
            }
        } catch (Exception ignored) {

        }
        return null;
    }

}